package com.xoms.admin.persist.impl;

import com.alibaba.fastjson.JSONArray;
import com.xoms.admin.controller.res.bean.*;
import com.xoms.admin.model.*;
import com.xoms.admin.persist.dao.RoomDao;
import com.xoms.util.StringUtil;
import org.apache.ibatis.session.ExecutorType;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: tzz(Tian)
 * Date: 2018/4/28 12:40
 */
@Repository
public class RoomImpl extends BaseImpl implements RoomDao {

    final String NAME_SPACE = NAME_SPACE_HEADER + ".RoomMapper";

    @Override
    public List<RoomDimensionBean> selectDimensions() {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDimensions");
    }

    @Override
    public List<RoomDimensionDetailBean> getDimensionDetailList(String dimensionId) {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDimensionDetails", dimensionId);
    }

    @Override
    public List<RRoomDimensionDetailBean> getDimensionAllDetailList() {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDimensionDetails");
    }

    @Override
    public List<RRoomDimensionScaleBean> getDimensionScaleDownList(String dimensionId, String did, String[] pids) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("dimensionId", dimensionId);
        params.put("did", did);
        params.put("pids", pids);
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDimensionScale", params);
    }

    @Override
    public List<RRoomDimensionScaleListBean> getDimensionScaleList() {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDimensionScales");
    }

    @Override
    public RoomDimensionDetailBean selectDimensionDetailById(String id) {
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDimensionDetailById", id);
    }

    @Override
    public RoomDimensionDetailBean selectDimensionDetailByName(String dimensionId, String name) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("dimensionId", dimensionId);
        params.put("name", name);
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDimensionDetailByName", params);
    }

    @Override
    public int insertDimensionDetail(RoomDimensionDetailBean data) {
        return sqlSessionTemplate.insert(NAME_SPACE + ".insertDimensionDetail", data);
    }

    @Override
    public int updateDimensionDetail(RoomDimensionDetailBean data) {
        return sqlSessionTemplate.update(NAME_SPACE + ".updateDimensionDetail", data);
    }

    @Override
    public int delDimensionDetail(String id) {
        return sqlSessionTemplate.delete(NAME_SPACE + ".deleteDimensionDetail", id);
    }

    @Override
    public RoomDimensionScaleBean selectDimensionScaleById(String id) {
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDimensionScaleById", id);
    }

    @Override
    public RoomDimensionScaleBean selectDimensionScaleByDims(String dimeRalation) {
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDimensionScaleByDims", dimeRalation);
    }

    @Override
    public int insertDimensionScale(RoomDimensionScaleBean data) {
        return sqlSessionTemplate.insert(NAME_SPACE + ".insertDimensionScale", data);
    }

    @Override
    public int updateDimensionScale(RoomDimensionScaleBean data) {
        return sqlSessionTemplate.update(NAME_SPACE + ".updateDimensionScale", data);
    }

    @Override
    public int delDimensionScale(String id) {
        return sqlSessionTemplate.delete(NAME_SPACE + ".deleteDimensionScale", id);
    }

    @Override
    public List<RRoomBean> getRoomList(String id, String no, String name, String scale, int page, int pagesize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("start", page * pagesize);
        params.put("length", pagesize);
        if (!StringUtil.isEmpty(no)) {
            params.put("no", no);
        }
        if (!StringUtil.isEmpty(name)) {
            params.put("name", name);
        }
        if (!StringUtil.isEmpty(scale)) {
            params.put("scale", scale.split(","));
        }
        if (!StringUtil.isEmpty(id)) {
            params.put("id", id);
        }
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectRoomList", params);
    }

    @Override
    public RoomBean selectRoomById(String id) {
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectRoomById", id);
    }

    @Override
    public int insertRoom(RoomBean data, String[] scales) {
        // 先插入关联关系
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("scales", scales);
        params.put("id", data.getRid());
        sqlSessionTemplate.insert(NAME_SPACE + ".insertRoomRelation", params);
        return sqlSessionTemplate.insert(NAME_SPACE + ".insertRoom", data);
    }

    @Override
    public int updateRoom(RoomBean data, String[] scales) {
        // 先删除再插入关联关系
        sqlSessionTemplate.delete(NAME_SPACE + ".deleteRoomRelation", data.getRid());
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("scales", scales);
        params.put("id", data.getRid());
        sqlSessionTemplate.insert(NAME_SPACE + ".insertRoomRelation", params);
        return sqlSessionTemplate.update(NAME_SPACE + ".updateRoom", data);
    }

    @Override
    public int delRoom(String id) {
        sqlSessionTemplate.delete(NAME_SPACE + ".deleteRoomRelation", id);
        return sqlSessionTemplate.delete(NAME_SPACE + ".deleteRoom", id);
    }

    @Override
    public List<RTimeBucketBean> getTimeBucketList() {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectTimeBucketList");
    }

    @Override
    public String selectDimeRelationByRoomId(String roomId) {
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDimeRelationByRoomId", roomId);
    }

    @Override
    public int addTimeBucket(TimeBucketBean data, List<Map<String, String>> list) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("list", list);
        params.put("name", data.getName());
        params.put("accountId", data.getAccountId());
        params.put("status", data.getStatus());
        params.put("theatreId", data.getTheatreId());
        return sqlSessionTemplate.insert(NAME_SPACE + ".insertTimeBucket", params);
    }

    @Override
    public RTimeBucketBean selectTimeBucketByName(String name) {
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectTimeBucketList", name);
    }

    @Override
    public int countOfTimeBucketRelation(String name) {
        return sqlSessionTemplate.selectOne(NAME_SPACE + ".countOfTimeBucketRelation", name);
    }

    @Override
    public int delTimeBucket(String name) {
        return sqlSessionTemplate.delete(NAME_SPACE + ".deleteTimeBucket", name);
    }

    @Override
    public List<RTimeBucketBean> getTimeBucket(String name) {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectTimeBucket", name);
    }

    @Override
    public int addDimePrice(List<RoomDimePriceBean> list, String scales) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("list", list);
        String[] dime = scales.replaceAll("[\\[|\\]|']", "").split(",");
        params.put("scales", dime);
        return sqlSessionTemplate.insert(NAME_SPACE + ".addDimePrice", params);
    }

    @Override
    public int addRoomPrice(List<RoomPriceBean> list) {
        return sqlSessionTemplate.insert(NAME_SPACE + ".addRoomPrice", list);
    }

    @Override
    public int addRoomPriceByDime(String roomId, String dimeId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("roomId", roomId);
        params.put("dimeId", dimeId);
        return sqlSessionTemplate.insert(NAME_SPACE + ".addRoomPriceByDime", params);
    }

    @Override
    public List<RRoomPriceBean> getRoomPriceList(String id, int page, int pagesize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("start", page * pagesize);
        params.put("length", pagesize);
        params.put("id", id);
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectRoomPrice", params);
    }

    @Override
    public List<RRoomMonthPriceBean> getRoomMonthPrice(Map<String, Object> params) {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectRoomMonthPrice", params);
    }

    @Override
    public List<RRoomPriceBean> getRoomPriceDetail(Map<String, Object> params) {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectRoomPriceDetail", params);
    }

    @Override
    public List<RRoomPriceBean> getRoomCurrPrice(String id) {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectRoomCurrPrice", id);
    }


    @Override
    public List<RDimePriceBean> getDimePriceList(String dateB, String dateE, Integer priceType, String scale, Integer page, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (!StringUtil.isEmpty(dateB)) {
            params.put("dateB", dateB);
        }
        if (!StringUtil.isEmpty(dateE)) {
            params.put("dateE", dateE);
        }
        if (priceType != null) {
            params.put("priceType", priceType);
        }
        if (!StringUtil.isEmpty(scale)) {
            params.put("scale", scale.split(","));
        }
        params.put("start", page * pageSize);
        params.put("length", pageSize);
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDimePriceList", params);
    }

    @Override
    public List<RDimePriceBean> getDimePriceDetail(Map<String, Object> params) {
        return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDimePriceDetail", params);
    }
}
